package com.zqq.lib_net

import android.annotation.SuppressLint
import android.content.Context
import android.content.Context.CONNECTIVITY_SERVICE
import android.net.ConnectivityManager
import android.net.ConnectivityManager.TYPE_WIFI
import android.net.Network
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
import android.net.NetworkCapabilities.TRANSPORT_CELLULAR
import android.net.NetworkCapabilities.TRANSPORT_VPN
import android.net.NetworkCapabilities.TRANSPORT_WIFI
import android.net.NetworkRequest
import android.os.Build
import android.provider.ContactsContract.CommonDataKinds.Email.TYPE_MOBILE

object NetUtils {

    @SuppressLint("ObsoleteSdkInt")
    fun isNetworkAvailable(context: Context): Boolean {
        val cm = context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val activeNetwork = cm.activeNetwork
            val capabilities = cm.getNetworkCapabilities(activeNetwork)
            capabilities?.hasCapability(NET_CAPABILITY_INTERNET) ?: false
        } else {
            @Suppress("DEPRECATION")
            cm.activeNetworkInfo?.isConnected ?: false
        }
    }

    fun getNetworkType(context: Context): String {
        val cm = context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val capabilities = cm.getNetworkCapabilities(cm.activeNetwork)
            when {
                capabilities == null -> "DISCONNECTED"
                capabilities.hasTransport(TRANSPORT_WIFI) -> "WIFI"
                capabilities.hasTransport(TRANSPORT_CELLULAR) -> "MOBILE"
                capabilities.hasTransport(TRANSPORT_VPN) -> "VPN"
                else -> "UNKNOWN"
            }
        } else {
            @Suppress("DEPRECATION")
            when (cm.activeNetworkInfo?.type) {
                TYPE_WIFI -> "WIFI"
                TYPE_MOBILE -> "MOBILE"
                else -> "UNKNOWN"
            }
        }
    }


    private val callback = object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) {
            // 网络恢复时触发
            callBack?.let { it(true) }
        }
        override fun onLost(network: Network) {
            // 网络断开时触发
            callBack?.let { it(false) }
        }
    }


    var callBack:((Boolean)->Unit)? = null
    /**
     * 监听网络状态
     */
    fun registerStataCallBack(context: Context,callBack:(Boolean)->Unit){
        this.callBack = callBack
        val cm = context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
        cm.registerDefaultNetworkCallback(callback)
    }

    /**
     * 页面销毁时调用
     */
    fun unregisterStateCallBack(context: Context) {
        val cm = context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
        cm.unregisterNetworkCallback(callback)
    }
}